package heven.holt.wan.android.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.paging.LoadState
import dagger.hilt.android.AndroidEntryPoint
import heven.holt.library.base.BaseFragment
import heven.holt.library.extensions.registerHeader
import heven.holt.library.loading.LoadingStateView
import heven.holt.library.loading.ViewType
import heven.holt.library.loading.delegate.ErrorViewDelegate
import heven.holt.library.loading.extensions.bindLoadingStateView
import heven.holt.library.paging.LoadStateFooterMoreAdapter
import heven.holt.wan.android.MainFragmentDirections
import heven.holt.wan.android.adapter.ArticleHomeAdapter
import heven.holt.wan.android.databinding.FragmentQaQuizBinding
import heven.holt.wan.android.viewmodel.QAQuizViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@AndroidEntryPoint
class QAQuizFragment : BaseFragment<FragmentQaQuizBinding>() {

    private lateinit var loadingStateView: LoadingStateView

    private val articleHomeAdapter = ArticleHomeAdapter()

    private val qaQuizViewModel: QAQuizViewModel by viewModels()

    override fun bindViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmentQaQuizBinding = FragmentQaQuizBinding.inflate(inflater, container, false)

    override fun createDecorationView(): View {
        loadingStateView = binding.root.bindLoadingStateView()
        loadingStateView.register(ViewType.ERROR, ErrorViewDelegate {
            onReload()
        })
        loadingStateView.showLoadingView()
        return loadingStateView.decorView
    }

    override fun initView() {
        binding.springView.registerHeader {
            onReload()
        }

        val concatAdapter =
            articleHomeAdapter.withLoadStateFooter(LoadStateFooterMoreAdapter {
                articleHomeAdapter.retry()
            })

        articleHomeAdapter.addLoadStateListener {
            if (isDetached) return@addLoadStateListener
            when (it.refresh) {
                is LoadState.Loading -> {
                }
                is LoadState.NotLoading -> {
                    loadingStateView.showContentView()
                }
                is LoadState.Error -> {
                    loadingStateView.showErrorView()
                }
            }
        }

        articleHomeAdapter.setOnItemClickListener { view, articleModel ->
            val url = articleModel.link ?: return@setOnItemClickListener
            val direction = MainFragmentDirections.actionGlobalWebViewFragment(url)
            view.findNavController().navigate(direction)
        }

        binding.recyclerView.adapter = concatAdapter
    }

    override fun initData() {
        lifecycleScope.launch {
            qaQuizViewModel.flow.collectLatest {
                binding.springView.onFinishFreshAndLoad()
                articleHomeAdapter.submitData(it)
            }
        }
    }

    private fun onReload() {
        articleHomeAdapter.refresh()
    }
}